<?php

namespace App\Models;

use App\Models\Rbac\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Order extends BaseModel
{
    use HasFactory;
    protected $table = 'order';
    // 允许批量操作的字段
    protected $fillable = [
        'modelid',
        'serial_number',
        'order_number',
        'order_parent_id',
        'num',
        'order_date',
        'delivery_date',
        'account_executive_id',
        'product_id',
        'model_number_id',
        'specification_id',
        'color_id',
        'direction',
        'customer_id',
        'receivables',
        'received',
        'remark',
        'emergency_procedure',
        'is_replenishment',
        'is_stock_up',
        'created_at',
        'updated_at',
        'status',
        'user_id',
        'id',
        'is_audit',
        'batch_number',
        'info_status',
        'material_status',
        'financial_status',
        'production_status',//生产状态：0：未生产1：生产中2：半成品3：成品
        'warehouse_id',
        'goods_shelf_id',
        'is_chuangtougui',
        'is_chuangbian',
        'is_chuangtou',
        'pid',
        'size_remark'
    ];

    /**
     * 订单列表
     * @param $query
     * @param $params
     * @return mixed
     */
    public function scopeSearch($query,$params)
    {
        //业务员
        if(!empty($params['account_executive_id'])){
            $query->where('account_executive_id',$params['account_executive_id']);
        }
        //客户
        if(!empty($params['customer_id'])){
            $query->where('customer_id',$params['customer_id']);
        }
        //产品
        if(!empty($params['product_id'])){
            $query->where('product_id',$params['product_id']);
        }
        //订单号
        if(!empty($params['order_number'])){
            $query->where('order_number',$params['order_number']);
        }
        //型号
        if(!empty($params['model_number_id'])){
            $query->where('model_number_id',$params['model_number_id']);
        }
        if(!empty($params['serial_number'])){
            $query->where('serial_number',$params['serial_number']);
        }

        //订单日期（默认当月）
//        $start_time = $params['start_time']??date("2023-01-01");
//        $end_time = $params['end_time']??date("Y-m-d",strtotime(date('Y-m')."+1 month -1 day"));
        if(!empty($params['start_time']) && !empty($params['end_time'])){
            $query->whereBetween('order_date',[$params['start_time'],$params['end_time']]);
        }

        if(!empty($params['order_id'])){
            $query->where('order_id',$params['order_id']);
        }

        //审核通过的订单
        $query->where('status',1)->where('is_del',2);
        return $query;
    }

    //订单审核
    public function scopeAudit($query,$params){
        //紧急程度
//        if(!empty($params['emergency_procedure'])){
//            $query->where('emergency_procedure',$params['emergency_procedure']);
//        }
        //业务员
//        if(!empty($params['account_executive_id'])){
//            $query->where('account_executive_id',$params['account_executive_id']);
//        }
        //客户
        if(!empty($params['customer_id'])){
            $query->where('customer_id',$params['customer_id']);
        }
        //产品
        if(!empty($params['product_id'])){
            $query->where('product_id',$params['product_id']);
        }
        //订单号
        if(!empty($params['order_number'])){
            $query->where('order_number',$params['order_number']);
        }

        //订单日期（默认当月）
        if(!empty($params['start_time']) && !empty($params['end_time'])){
            $query->whereBetween('order_date',[$params['start_time'],$params['end_time']]);
        }
        //流水号
        if(!empty($params['serial_number'])){
            $query->where('serial_number',$params['serial_number']);
        }

        if($params['role_id'] == 1){
            $query->where('status','<>',1);
        }else{
            $orderAudit = OrderAudit::where('status',1)->get();
            foreach($orderAudit as $k=>$v){
                if($v->role_id == $params['role_id']){
                    $query->where($v->field_name,'<>',1);
                }
            }
        }
        $query->where('is_del',2);
        return $query;

    }

    /**生产进度**/
    public function scopeSchedule($query,$params){
        //业务员
        if(!empty($params['account_executive_id'])){
            $query->where('account_executive_id',$params['account_executive_id']);
        }
        //客户
        if(!empty($params['customer_id'])){
            $query->where('customer_id',$params['customer_id']);
        }
        //产品
        if(!empty($params['product_id'])){
            $query->where('product_id',$params['product_id']);
        }
        //订单号
        if(!empty($params['order_number'])){
            $query->where('order_number',$params['order_number']);
        }
        //型号
        if(!empty($params['model_number_id'])){
            $query->where('model_number_id',$params['model_number_id']);
        }
        //流水号
        if(!empty($params['serial_number'])){
            $query->where('serial_number',$params['serial_number']);
        }

        //订单日期（默认当月）
        if(!empty($params['start_time']) && !empty($params['end_time'])){
            $query->whereBetween('order_date',[$params['start_time'],$params['end_time']]);
        }

        if(!empty($params['order_id'])){
            $query->where('id',$params['order_id']);
        }

        //不是补录订单
        $query->where('is_replenishment',2);
        //审核通过的订单
        $query->where('status',1)->where('is_del',2);
        $query->whereIn('production_status',[0,1]);
        return $query;
    }


    public function color(){
        return $this->hasOne(Color::class,'id','color_id');
    }

    public function product(){
        return $this->hasOne(Product::class,'id','product_id');
    }

    public function product_name(){
        return $this->hasOne(Product::class,'id','product_id');
    }


    public function modelNumber(){
        return $this->hasOne(ModelNumber::class,'id','model_number_id');
    }

    public function model_number_name(){
        return $this->hasOne(ModelNumber::class,'id','model_number_id');
    }

    public function customer(){
        return $this->hasOne(Customer::class,'id','customer_id');
    }

    public function orderChild(){
        return $this->hasMany(OrderChild::class,'order_id','id');
    }

    //业务员
    public function accountExecutive(){
        return $this->hasOne(User::class,'id','account_executive_id');
    }

    public function specification(){
        return $this->hasOne(Specification::class,'id','specification_id');
    }
    //审核记录
    public function audit_log(){
        return $this->hasMany(OrderAuditLog::class,'order_id','id');
    }

    public function finish_product(){
        return $this->belongsTo(FinishProduct::class,'id','order_id');
    }
    public function admin_user(){
        return $this->hasOne(User::class,'id','user_id');
    }

    //盘点记录
    public function check_log(){
        return $this->hasMany(CheckLog::class,'order_id','id');
    }

    public function order_second(){
        return $this->hasMany(OrderSecond::class,'order_id','id');
    }

    public function warehouse(){
        return $this->hasOne(Warehouse::class,'id','warehouse_id');
    }



}
